//
//  LGInspectionReportCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/9.
//

#import "LGInspectionReportCellOld.h"

@interface LGInspectionReportCellOld()
/**   */
@property (nonatomic , strong)UIView *shadowView;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *warningLabel;
/**   */
@property (nonatomic , strong)UIButton *checkButton;
/**   */
@property (nonatomic , strong)UIButton *deleteButton;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)UIButton *countButton;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)NSArray *photoArray;
/**   */
@property (nonatomic , assign)CGFloat photoW;

@end

@implementation LGInspectionReportCellOld

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSInteger count = isPad?8:4;
        self.photoW = (CGFloat)(Screen_W-viewPix(76)-viewPix(4)*(count-1))/count;
        [self createSubView];
    }
    return  self;
}

- (void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *type = LGNSString(dataDic[@"type"]);
    self.checkButton.selected = [dataDic[@"isfinish"] boolValue];
    self.checkButton.userInteractionEnabled = [dataDic[@"isfinish"] boolValue];
    NSString *borderColor = ([dataDic[@"isfinish"] boolValue]==YES)?@"#1F6CC6":@"#CCCCCC";
    self.checkButton.layer.borderColor = [UIColor colorWithString:borderColor].CGColor;
    self.checkButton.hidden = [dataDic[@"type"] isEqualToString:@"system"]?YES:NO;
    self.deleteButton.hidden = [dataDic[@"type"] isEqualToString:@"doctor"]?NO:YES;
    NSString *title = LGNSString(dataDic[@"exams_code_str"]);
    NSString *warnSting = LGNSString(dataDic[@"warnitmes"]);
    self.titleLabel.text = title;
    self.warningLabel.text = warnSting;
    self.photoArray = [dataDic[@"type"] isEqualToString:@"his"]?[NSArray array]:dataDic[@"images"];
    CGFloat titleHeight = [title textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(80)];
    CGFloat photoHeight = (self.photoArray.count>0)?(self.photoW+viewPix(50)):viewPix(8);
    CGFloat warnHeight = [warnSting textHeightWithFont:LGFont(13) maxWidth:Screen_W-viewPix(80)];
    CGFloat warnTop = photoHeight;
    CGFloat checkTop = (warnHeight>0)?(warnTop+warnHeight+viewPix(10)):warnTop;
    
    [self.warningLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(warnTop);
    }];
    
    [self.checkButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(checkTop);
    }];
    
    self.photoView.hidden = (self.photoArray.count>0)?NO:YES;
    self.countButton.hidden = (self.photoArray.count>4)?NO:YES;
    self.numberLabel.hidden = (self.photoArray.count>0)?NO:YES;
    
    CGFloat bottom = [type isEqualToString:@"system"] ? viewPix(15):viewPix(55);
 
    CGFloat baseHeight = checkTop+bottom+titleHeight;
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(baseHeight));
    }];
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    for (UIImageView *imageView in self.photoView.subviews) {
        [imageView removeFromSuperview];
    }
    
    if(photoArray.count == 0) return;
    
    NSInteger maxCount = isPad?8:4;
    NSInteger count = (photoArray.count>maxCount)?maxCount:photoArray.count;
    for (NSInteger i=0; i<count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.photoW+viewPix(4))*i, 0, self.photoW, self.photoW)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i]]];
        imageView.userInteractionEnabled = YES;
        imageView.cornerRidus = 3;
        imageView.tag = 1000+i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
        [self.photoView addSubview:imageView];
    }
    [self.photoView addSubview:self.countButton];
    self.countButton.hidden = (photoArray.count>maxCount)?NO:YES;
    [self.countButton setTitle:[NSString stringWithFormat:@"+%ld",photoArray.count] forState:UIControlStateNormal];
    self.countButton.frame = CGRectMake((self.photoW+viewPix(4))*(maxCount-1), 0, self.photoW, self.photoW);
    [self.photoView bringSubviewToFront:self.countButton];
    self.numberLabel.text = [NSString stringWithFormat:@"共 %ld 张图片",photoArray.count];
}

-(void)countButtonAction{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect rect = [self.countButton convertRect:self.frame toView:window];
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkPhotoWithPoint:index:photoArray:)]){
        [self.delegate checkPhotoWithPoint:CGPointMake(rect.origin.x, rect.origin.y) index:0 photoArray:self.photoArray];
    }
}

-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect rect = [sender.view convertRect:self.frame toView:window];
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkPhotoWithPoint:index:photoArray:)]){
        [self.delegate checkPhotoWithPoint:CGPointMake(rect.origin.x, rect.origin.y) index:imageView.tag-1000 photoArray:self.photoArray];
    }
}

//查看报告详情
-(void)checkButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkReportDetail:)]){
        [self.delegate checkReportDetail:self.dataDic];
    }
}

//删除报告
-(void)deleteButtonAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(deleteReportAction:)]){
        [self.delegate deleteReportAction:self.dataDic];
    }
}

#pragma mark
#pragma mark ====> 创建控件

-(void)createSubView{
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self.contentView addSubview:self.shadowView];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.warningLabel];
    [self.baseView addSubview:self.checkButton];
    [self.baseView addSubview:self.deleteButton];
    [self.baseView addSubview:self.photoView];
    [self.baseView addSubview:self.numberLabel];
    [self.photoView addSubview:self.countButton];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(-viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(viewPix(8));
    }];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(6));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        make.height.equalTo(@(viewPix(85)+self.photoW));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(12));
        make.width.equalTo(@(Screen_W-viewPix(64)));
        make.height.equalTo(@(self.photoW));
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoView.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.photoView);
    }];
    [self.warningLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(8));
        make.left.right.equalTo(self.titleLabel);
    }];
    [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(8));
        make.width.equalTo(@(85));
        make.height.equalTo(@(30));
    }];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkButton.mas_right).offset(viewPix(8));
        make.width.height.centerY.equalTo(self.checkButton);
    }];
    
}

-(UIView *)shadowView{
    if(!_shadowView){
        _shadowView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:8];
        _shadowView.backgroundColor = [UIColor whiteColor];
    }
    return _shadowView;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor colorWithString:@"#F5F6FA"];
        _baseView.cornerRidus = 7;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
//        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(12), Screen_W-viewPix(36)-viewPix(24), viewPix(22));
    }
    return _titleLabel;
}

-(UILabel *)warningLabel{
    if(!_warningLabel){
        _warningLabel = [UILabel lableWithText:@"" colorString:@"#EB5757" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _warningLabel;
}

-(UIButton *)checkButton{
    if(!_checkButton){
        _checkButton = [UIButton buttonWithTitle:@"查看详情" titleFont:LGFont(16) normalColor:@"#CCCCCC" selectColor:@"#1F6CC6"];
        _checkButton.layer.borderColor = [UIColor colorWithString:@"#CCCCCC"].CGColor;
        _checkButton.layer.borderWidth = 1;
        _checkButton.cornerRidus = viewPix(15);
        [_checkButton addTarget:self action:@selector(checkButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _checkButton;
}

-(UIButton *)deleteButton{
    if(!_deleteButton){
        _deleteButton = [UIButton buttonWithTitle:@"删除" titleFont:LGFont(16) normalColor:@"#DD3030" selectColor:@"#DD3030"];
        _deleteButton.layer.borderColor = [UIColor colorWithString:@"#DD3030"].CGColor;
        _deleteButton.layer.borderWidth = 1;
        _deleteButton.cornerRidus = viewPix(15);
        [_deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(12), viewPix(45), Screen_W-viewPix(64), self.photoW)];
        _photoView.backgroundColor = [UIColor colorWithString:@"#F5F6FA"];
    }
    return _photoView;
}

-(UIButton *)countButton{
    if(!_countButton){
        _countButton = [UIButton buttonWithTitle:@"+3" titleFont:LGFontWeight(22, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _countButton.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        _countButton.cornerRidus = 3;
        _countButton.hidden = YES;
        [_countButton addTarget:self action:@selector(countButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _countButton;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"共 6 张图片" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _numberLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
